package com.example.myspringboot.business.schedule;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;

import javax.annotation.Resource;

/**
 * 通过实现更细致的配置类设置定时任务线程池，可以通过该类进行更细致的定时任务管理
 *
 * @author nagiumi
 * @date 2023/11/23
 */
//@Configuration
public class ScheduleConfigurer implements SchedulingConfigurer {

    @Resource
    private ScheduleTest scheduleTest;

    @Override
    public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
        taskRegistrar.addCronTask(() -> scheduleTest.run3(), "* * * * * ?");
        taskRegistrar.addCronTask(() -> scheduleTest.run4(), "0/5 * * * * ?");
        taskRegistrar.setTaskScheduler(configurerScheduler());
    }

//    @Bean
    public ThreadPoolTaskScheduler configurerScheduler() {
        ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
        scheduler.setPoolSize(4);
        scheduler.setThreadNamePrefix("nagiumi-configurer-scheduler-");
        return scheduler;
    }
}
